import type { RouteRecordRaw } from "vue-router";
import { createRouter, createWebHistory } from "vue-router";
import {
  routerRef,
  Layout,
  LoginPage,
  DashboardPage,
  MultiFactorLoginPage,
  ProfilePage,
  Error401Page,
  Error404Page,
  RedirectPage,
} from "jmash-core";

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: RedirectPage,
      },
    ],
  },
  {
    path: "/login",
    component: LoginPage,
    meta: { hidden: true },
  },
  {
    path: "/secondLogin",
    component: MultiFactorLoginPage,
    meta: { hidden: true, title: "手机号/邮箱登录" },
  },
  {
    path: "/my",
    component: Layout,
    meta: { hidden: true },
    redirect: "noredirect",
    children: [
      {
        path: "profile",
        component: ProfilePage,
        name: "Profile",
        meta: { title: "个人中心", icon: "user" },
      },
    ],
  },

  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "/dashboard",
        component: DashboardPage,
        name: "Dashboard", // 用于 keep-alive, 必须与SFC自动推导或者显示声明的组件name一致
        // https://cn.vuejs.org/guide/built-ins/keep-alive.html#include-exclude
        meta: {
          title: "Dashboard",
          icon: "homepage",
          affix: true,
          keepAlive: true,
          alwaysShow: false,
        },
      },
      {
        path: "401",
        component: Error401Page,
        meta: { hidden: true },
      },
      {
        path: "404",
        component: Error404Page,
        meta: { hidden: true },
      },
    ],
  },
];

/**
 * 创建路由
 */
const router =createRouter({
  history: createWebHistory(import.meta.env.VITE_APP_BASE_URL),
  routes: constantRoutes as RouteRecordRaw[],
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

routerRef.value = router;


export default router;
